// src/stores/user.js
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const user = defineStore('user', () => {
    // 状态
    const token = ref(localStorage.getItem('token') || null)
    const isLoggedIn = ref(!!token.value)
    const userInfo = ref(null)

    // 动作
    function setToken(newToken) {
        token.value = newToken
        isLoggedIn.value = true
        localStorage.setItem('token', newToken)
    }

    function setUserInfo(info) {
        userInfo.value = info
    }

    function logout() {
        token.value = null
        isLoggedIn.value = false
        userInfo.value = null
        localStorage.removeItem('token')
    }

    return {
        token,
        isLoggedIn,
        userInfo,
        setToken,
        setUserInfo,
        logout
    }
}, {
    persist: true, // 启用持久化（需 pinia-plugin-persistedstate）
})